<div>
    <button data-controls-modal="modal-new-menu" data-backdrop="true" class="btn success">Add new menu</button>
</div>
<br />
<?php

function print_menus($menus, $left = '') {
    foreach ($menus as $menu) { ?>
        <tr class="wrapper menu_item">
        <form method="post">
            <?php echo Form::hidden('id', $menu->id); ?>
            <td style="text-align: center;">
                <?php echo $menu->id ?>
            </td>
            <td>
                <?php echo $left; ?><input class="span4" name="title" value="<?php echo $menu->title; ?>" />
            </td>
            <td>
                <input class="span8" name="uri" value="<?php echo $menu->uri; ?>" />
            </td>
            <td style="text-align: right;">
                <button class="btn primary" type="submit" name="save" value="true">Save</button>
                <button class="btn error" type="submit" name="remove" value="true">Remove</button>
                <input type="submit" name="up" value="&uparrow;" class="btn" />
                <input type="submit" name="down" value="&downarrow;" class="btn" />
            </td>
        </form>
        </tr>
        <?php
        $childs = $menu->childs->order_by('pos')->find_all();
        if ($childs)
            print_menus($childs, $left . '&RightTee;&nbsp;');
    }
}
?>
<table>
    <tr>
        <th>id</th>
        <th>title</th>
        <th>uri</th>
        <th></th>
    </tr>
    <?php
    print_menus(ORM::factory('menu')
                    ->where('parent_id', '=', 0)
                    ->order_by('pos')
                    ->find_all());
    ?>
</table>

<div id="modal-new-menu" class="modal hide fade">
    <div class="modal-header">
        <a href="#" class="close">&times;</a>
        <h3>New menu</h3>
    </div>
    <form method="post">
        <div class="modal-body">
            <input type="hidden" name="action" value="add_new" />
            <div class="clearfix">
                <label>Parent id</label>
                <div class="input">
                    <input name="parent_id" value="0" required />
                </div>
            </div>
            <div class="clearfix">
                <label>Title</label>
                <div class="input">
                    <input name="title" value="" required />
                </div>
            </div>
            <div class="clearfix">
                <label>URI</label>
                <div class="input">
                    <input class="span6" name="uri" value="" required />
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn primary" type="submit">Create</button>
        </div>
    </form>
</div>